<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="服务器ip" prop="localServerIp">
        <el-input
          v-model="queryParams.localServerIp"
          placeholder="请输入服务器ip"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采集时间" prop="collectionTime">
        <el-date-picker clearable
          v-model="queryParams.collectionTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择采集时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="日志级别">
        <el-select v-model="queryParams.level" placeholder="请选择日志级别" clearable>
          <el-option
            v-for="item in levelOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="logCollectionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="服务器ip" align="center" prop="localServerIp" />
      <el-table-column label="采集时间" align="center" prop="collectionTime">
        <template slot-scope="scope">
          <span>{{ scope.row.collectionTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="日志级别" align="center" prop="level"  width="100" />
      <el-table-column label="概览" align="center" prop="resume"  width="350" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
          >查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改日志采集对话框 -->
    <el-dialog width="80%" :visible.sync="open" append-to-body>
      <div style="padding-left: 20px; max-height:450px;  overflow: auto"><pre v-html="form.detail"></pre></div>
    </el-dialog>

  </div>
</template>

<script>
import { listLogCollection, getLogCollection } from "@/api/log/logCollection";

export default {
  name: "LogCollection",
  data() {
    return {
      levelOptions: [{
        value: 'ERROR',
        label: 'ERROR'
      }, {
        value: 'WARN',
        label: 'WARN',
      }, {
        value: 'INFO',
        label: 'INFO'
      }, {
        value: 'DEBUG',
        label: 'DEBUG'
      }],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日志采集表格数据
      logCollectionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applyName: null,
        resume: null,
        level: null,
        detail: null,
        localServerIp: null,
        collectionTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询日志采集列表 */
    getList() {
      this.loading = true;
      this.queryParams.tableName = this.$route.query.tableName;
      listLogCollection(this.queryParams).then(response => {
        this.logCollectionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        applyName: null,
        resume: null,
        level: null,
        detail: null,
        localServerIp: null,
        collectionTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.level = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看详情按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids;
      const tableName = this.$route.query.tableName;
      const query = {
        "tableName":tableName,
        "id":id
      }
      getLogCollection(query).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改日志采集";
      });
    },
  }
};
</script>

